import { GameManager } from "./GameManager";
import { HookCollider } from "./HookCollider";
import { ShopItemManager } from "./ShopItemManager";


const { ccclass, property } = cc._decorator;

let ROPE_STATE = cc.Enum({
    ADD: 1,
    REDUCE: 2,
    WAIT: 3
});

@ccclass
export class Hook extends cc.Component {

    isRotating: boolean = true; //是否旋转中
    rotateSpeed: number = 60; //旋转速度
    canvas: cc.Node;
    public ropeState: number;
    ropeOriginalHeight: number;
    hookColliderScript: HookCollider;
    ropeSpeed: number;

    @property(cc.Node)
    hookItem: cc.Node;

    gameManager;

    start() {
        this.gameManager = GameManager.Instance(GameManager);
        this.gameManager.updateUI();
        this.gameManager.endGamePanel.active = false;
        this.gameManager.ropeSpeed = this.gameManager.normalRopeSpeed;
        this.gameManager.ropeState = ROPE_STATE.WAIT;
        this.ropeSpeed = GameManager.Instance(GameManager).ropeSpeed;
        this.node.angle = -50
        this.canvas = cc.find("Canvas");
        this.ropeOriginalHeight = this.node.height;
        this.hookColliderScript = this.hookItem.getComponent(HookCollider);
        this.canvas.on(cc.Node.EventType.TOUCH_END, this.sendHook.bind(this));
    }

    update(deltaTime) {
        this.rotateHook(deltaTime)
        this.hookLengthen(deltaTime);
    }

    /**
     * 旋转钩子
     * @param deltaTime 
     */
    rotateHook(deltaTime) {
        if (!this.isRotating) {
            return;
        }
        if (this.node.angle >= 60) {
            this.rotateSpeed = -this.rotateSpeed;
        } else if (this.node.angle <= -60) {
            this.rotateSpeed = Math.abs(this.rotateSpeed);
        }

        this.node.angle += this.rotateSpeed * deltaTime;
    }

    /**
     * 发射钩子
     */
    sendHook() {
        this.isRotating = false;

        if (this.gameManager.ropeState == ROPE_STATE.ADD) {
            this.gameManager.ropeState = ROPE_STATE.REDUCE;
            if (GameManager.Instance(GameManager).catchItem) {
                GameManager.Instance(GameManager).playAnim("pullHeavy");
            }
        }
        if (this.gameManager.ropeState == ROPE_STATE.WAIT) {
            this.gameManager.ropeState = ROPE_STATE.ADD;
        }
    }

    /**
     * 绳子长度改变 
     */
    hookLengthen(deltaTime) {
        if (this.gameManager.ropeState == ROPE_STATE.ADD) {
            this.node.height += 100 * deltaTime;
        } else if (this.gameManager.ropeState == ROPE_STATE.REDUCE) {
            this.node.height -= GameManager.Instance(GameManager).ropeSpeed * deltaTime;
            if (this.node.height <= this.ropeOriginalHeight) {
                this.gameManager.ropeState = ROPE_STATE.WAIT;
                this.isRotating = true;
                this.node.height = this.ropeOriginalHeight;
                this.node.angle = 0;
                let gameManager = GameManager.Instance(GameManager);
                gameManager.ropeSpeed = 100;
                gameManager.stopAnim();
                if (gameManager.catchItem) {
                    let score = gameManager.catchItem.score;
                    cc.log(gameManager.catchItem)
                    if (gameManager.catchItem.name.indexOf('stone') !== -1 && ShopItemManager.Instance(ShopItemManager).stoneValueUpCount >0) {
                        ShopItemManager.Instance(ShopItemManager).stoneValueUpCount -= 1;
                        score *= 3;
                    }
                    gameManager.increseMoney(score);
                    gameManager.catchItem = null;
                    this.hookItem.removeAllChildren();
                }
            }
        }
    }

    /**
     * 产生随机整数，包含下限值，包括上限值
     * 
     * @param lower 
     * @param upper 
     */
    randomNum(lower, upper) {
        return Math.floor(Math.random() * (upper - lower + 1)) + lower;
    }

}
